'use strict';

/* Controllers */
app.controller('loansStr', function($scope,$compile,ngDialog,uiLoad,$state,user,rest,$ocLazyLoad ) {
   $scope.queryParams = {};
   $scope.subject1 = {};
    $scope.queryParams = {
         'openId':user.get('openId'),
       // 'openId':'1dca25eabc753b7087ef659a5d7c5c98',            
        'startTime': $scope.startTime,
        'endTime': $scope.endTime,
        'nameCode': $scope.nameCode
    }
   rest.post({
        methodId: 'J018',         
         data: {'accountOpenId': user.get('openId')},
       // data: {'accountOpenId': '1dca25eabc753b7087ef659a5d7c5c98'},
        success: function(res) {
           // console.log(res);
           var numAmount = res.data.realAmount.substring(0,res.data.realAmount.length -2);    
           $scope.totalAmount = numAmount;
           $scope.totalAmountLeft = numAmount;
           $scope.$apply();
        },
        error: function() {
           
        }
    })
    $scope.checkType = {};
    $scope.fundTypeArray = [{ "name": "全部类型", "value": "" }, { "name": "收入", "value": "收入" }, { "name": "支出", "value": "支出" }];
    function time(mydate){
      var str = mydate.substring(0,10);
      var dataArr = str.split('-');
      var timeStr = dataArr[0]
        +"年"+dataArr[1]+"月"+dataArr[2]
        +"日";
        return timeStr;
    } 
    function simplifyNum(mydata,mysubject){       
      var str = mydata.substring(0,mydata.length -2);     
       if(mysubject == '收入'){
           str = '<span class = "red">+&nbsp;&nbsp;'  +str + '</span>'
       }else{
            str = '<span class = "green">-&nbsp;&nbsp;'  +str + '</span>'
       }
       return str;
    }    
    function simplifyNum2(mydata){       
      var str = mydata.substring(0,mydata.length -2);         
       return str;
    }    
   
    $scope.queryUser = function(){ 
            
          if($scope.queryParams.subject1 && $scope.queryParams.subject1.selected){           
            if($scope.queryParams.subject1.selected){
                $scope.subject1 = $scope.queryParams.subject1.selected.value
            }
            $scope.queryParams = {
                'openId':user.get('openId'),
                // 'openId':'1dca25eabc753b7087ef659a5d7c5c98',                
                'subject1': $scope.subject1,
                'dateStart': $scope.startTime,
                'dateEnd': $scope.endTime,
                'nameCode': $scope.nameCode
            }
            $scope.dataTable.fnClearTable();
            $scope.queryParams = {};           
        }
        else{
            $scope.queryParams = {
                'openId':user.get('openId'),
                //'openId':'1dca25eabc753b7087ef659a5d7c5c98',                   
                'dateStart': $scope.startTime,
                'dateEnd': $scope.endTime,
                'nameCode': $scope.nameCode
            }
            console.log($scope.queryParams);
            $scope.dataTable.fnClearTable();                
        }                 
    };
   
    $scope.tableFundListDatas = {
        "sAjaxSource": "J019",
        "aoColumns": [{
            "mDataProp": "id",
            "sTitle": "流水号",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "subject2",
            "sTitle": "科目",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp": "subject1",
            "sTitle": "类型",
            "sDefaultContent": "",
            "sClass": "center"
        },{
            "mDataProp": "amountABS",
            "sTitle": "收入/支出（元）",
            "sDefaultContent": "",
            "sClass": "center amount-abs"
        // },  {
        //     "mDataProp": "realAmount",
        //     "sTitle": "余额（元）",
        //     "sDefaultContent": "",
        //     "sClass": "center realAmount"
        }, {
            "mDataProp": "createTime",
            "sTitle": "交易时间",
            "sDefaultContent": "",
            "sClass": "center rentAmount-time"
        }],
        "fnRowCallback": function(nRow, aData, iDisplayIndex ) {   
            $(nRow).find('.realAmount').empty().append(simplifyNum2(aData.realAmount));     
            $(nRow).find('.create-time').empty().append(time(aData.createTime));            
            $(nRow).find('.amount-abs').empty().append(simplifyNum(aData.amountABS,aData.subject1));            
        },
        "fnDrawCallback": function(oSettings) {
            $scope.subject1Value = $scope.queryParams.subject1;
            if ("number" == typeof $scope.subject1Value) {
                $scope.checkTypeArray.map(function(ele, index) {
                    if ($scope.subject1Value == ele.value) {
                        $scope.subject1.selected = ele;
                    }
                })
            }
        },
        "fnServerParams": function(aoData) {
          if($scope.subject1 && $scope.subject1.selected){
              $scope.queryParams.subject1 = $scope.subject1.selected.value
          }
          aoData.push({
              'name': 'queryParams',
              'value': $scope.queryParams
          });
          return aoData;
        },
        "fnServerData": function(sSource,aData,fnCallback,dataTableObject) {
           var obj = dataTableObject.oAjaxData,_baseParams = {
             'landlordId':user.get('landlordId'),
             // 'landlordId':'ec1a71e8-622d-45b4-b256-2b037ea64458',
             'page':obj.iDisplayStart / obj.iDisplayLength + 1,
             'rows':obj.iDisplayLength
           };
           var __data = angular.extend({},_baseParams,obj.queryParams);
           rest.post({
             methodId: sSource,
             data: __data,
           
             success: function(data){                  
               if("0" == data.code){
                 var data = {
                     'aaData': data.res.data.rows,
                     'sEcho': obj.sEcho,
                     'iTotalDisplayRecords': data.res.data.total,
                     'iTotalRecords': data.res.data.pageCount,
                     'success': data.code == "0" ? true : false
                 };
                 fnCallback(data);
               }
             }
           })
         }

    };
    $scope.showMes = 1;
    $scope.successfulStr1 = function() {
        $scope.showMes = 0;
     }
     
     $scope.withdraw = function() {
          ngDialog.openConfirm({
          template: 'js/module/fund/withdraw.html',
          resolve: {
            load: function() {
                return uiLoad.load(['js/module/fund/withdraw.js'])
            }
          },
          data:{
            
          },
          scope: $scope,
          width: 600,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });
     }
     
    $scope.dataTable = undefined;

          // "landlordId": "40155814-52b1-4cf2-814a-724f770bbd5b",
          // "decorateId": "5bea9b36-1f94-4bf6-af8a-e2ed3117f53b",
          // "houseId": "e55c6e9f-d6b4-46d0-94c5-e20063ee0611",

    // 回调函数,返回实例化的jquery 插件对象
    $scope.initFinish = function(dataTable) {
        $scope.dataTable = dataTable;
    }   
    $scope.$on('fundStartDate1', function(event, data) {
        $scope.startTime = data ? (data + " 00:00:00") : "";
        console.log(data);
    });
    $scope.$on('fundEndDate2', function(event, data) {
        $scope.endTime = data ? (data + " 00:00:00") : "";
        console.log(data);
    });
});


app.controller('fundListTimeCtrl',function($scope){
  $scope.today = function() {
      $scope.fundStartDate = "";
      $scope.fundEndDate = "";
      // $scope.minStartDate = moment().subtract(30, 'days');
      // $scope.maxStartDate = moment();
    };

    $scope.closeAll = function() {
      $scope.openContractBeginDated =
      $scope.openContractEndDated = false;
    }

    $scope.openContractBeginDate = function($event) {
      $event.preventDefault();$event.stopPropagation();$scope.closeAll();
      $scope.openContractBeginDated = true;
    };
    $scope.openContractEndDate = function($event) {
      $event.preventDefault();$event.stopPropagation();$scope.closeAll();
      $scope.openContractEndDated = true;
    };
    
     $scope.today();
     
    $scope.dateOptions = {
      format: 'yyyy/MM/dd',
      startDay: 1,
      class: 'datepicker',
      endDate:'0d'
      
    };

    $scope.format = 'yyyy/MM/dd';

    

    $scope.$watch('fundStartDate',function(newValue,oldValue){
      if("[object Date]" === Object.prototype.toString.call(newValue)) {
         $scope.$emit('fundStartDate1',moment(newValue).format('YYYY-MM-DD'))
      }else if (null === newValue) {
        $scope.$emit('fundStartDate1','')
      }
    });
    $scope.$watch('fundEndDate',function(newValue,oldValue){
      if("[object Date]" === Object.prototype.toString.call(newValue)) {
         $scope.$emit('fundEndDate2',moment(newValue).add(1,'days').format('YYYY-MM-DD'))
      }else if (null === newValue) {
        $scope.$emit('fundEndDate2','')
      }
    });
});
